package com.proxy.autoclick.clicker.controller;

import java.sql.SQLException;

import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;

import com.proxy.autoclick.clicker.thread.ClickerThreadPool;
import com.proxy.autoclick.common.util.DataSourceUtil;
import com.proxy.autoclick.ui.layout.MainFrame;

@Named
public class ClickerController {
	
	@Inject
	private ClickerThreadPool clickerThreadPool;
	
	@Inject
	private MainFrame mainframe;
	
	private PoolingClientConnectionManager connectionManager;
	private HttpClient httpclient;
	
	public ClickerController()
	{
		// Create an HttpClient with the ThreadSafeClientConnManager.
		// This connection manager must be used if more than one thread will
		// be using the HttpClient.
		connectionManager = new PoolingClientConnectionManager();
		connectionManager.setMaxTotal(50);
		connectionManager.setDefaultMaxPerRoute(5);
		
		httpclient = new DefaultHttpClient(connectionManager);
		
	}
	
	public boolean startCheckAndClick() throws Exception
	{
		/*List<Proxy> myProxyList = clickerDao.getValidProxyList();
		if(myProxyList.isEmpty())
		{
			mainframe.showMessage("There is no valid proxy.");
			return Boolean.FALSE;
		}*/
		
		while(true)
		{
			clickerThreadPool.start(httpclient);
			mainframe.setTextAreaText("\r\nProcessing source txt file again.");
			Thread.sleep(200);
		}
		
//		return Boolean.TRUE;
	}
	
	@PreDestroy
	public void stopCheckandClick() throws SQLException
	{
		httpclient.getConnectionManager().shutdown();
		DataSourceUtil.closeDataSource();
	}
	

}
